package com.hnichr.ump.plan.controller;


import com.hnichr.ump.common.plan.entity.PlanScheduleContents;
import com.hnichr.ump.common.plan.entity.PlanScheduleContentsAttach;
import com.hnichr.ump.common.plan.service.PlanScheduleContentsService;
import com.hnichr.ump.common.plan.vo.ContentAttachVo;
import com.hnichr.ump.common.plan.vo.PlanScheduleContentsVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plan/schedule/content/v1/")
@Tag(name = "PlanSchedulesContentController", description = "重点工作内容接口")
public class PlanSchedulesContentController {

    @Autowired
    private PlanScheduleContentsService planScheduleContentsService;

    @GetMapping("list")
    @Operation(summary = "重点工作内容列表", description = "获取重点工作内容列表（无分页）", method = "GET")
    public Result<List<PlanScheduleContents>> list(@Parameter(description="查询参数") Criteria criteria){
        try{
            List<PlanScheduleContents> list = planScheduleContentsService.list(criteria);
            return Result.OK(list);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }

    }

    @GetMapping("page")
    @Operation(summary = "重点工作内容列表", description = "重点工作内容列表（分页）", method = "GET")
    public Result<Page<PlanScheduleContents>> page(@Parameter(description="查询参数")  Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        try{
            Pageable page = PageRequest.of(pageNo-1, pageSize);
            Page<PlanScheduleContents> list = planScheduleContentsService.listPage(page, criteria);
            return Result.OK(list);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }

    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取重点工作内容", description = "根据ID获取重点工作内容", method = "GET")
    public Result<PlanScheduleContents> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        PlanScheduleContents schedules = planScheduleContentsService.getById(id);
        return Result.OK(schedules);
    }

    @PostMapping("add")
    @Operation(summary = "新增重点工作内容", description = "新增重点工作内容", method = "POST")
    public Result<PlanScheduleContents> add(@RequestBody PlanScheduleContentsVo schedules) {
        try{
            PlanScheduleContents content = planScheduleContentsService.save(schedules);
            return Result.OK(content);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("edit")
    @Operation(summary = "编辑重点工作内容", description = "编辑重点工作内容", method = "PUT")
    public Result<PlanScheduleContents> edit(@RequestBody PlanScheduleContentsVo schedules) {
        try{
            PlanScheduleContents content = planScheduleContentsService.edit(schedules);
            return Result.OK(content);
        }
        catch (Exception e){
            return Result.error(e.getMessage());
        }

    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除重点工作内容", description = "根据id删除重点工作内容", method = "DELETE")
    public Result<PlanScheduleContents> remove(@PathVariable(name = "ids", required = true) String ids) {
        planScheduleContentsService.remove(ids);
        return Result.OK();
    }

    @PutMapping("accomplish/{id}")
    @Operation(summary = "此节点完成", description = "此节点完成", method = "PUT")
    public Result<PlanScheduleContents> accomplish(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        PlanScheduleContents schedules = planScheduleContentsService.accomplish(id);
        return Result.OK(schedules);
    }

    @PutMapping("unaccomplished/{id}")
    @Operation(summary = "此节点改为未完成", description = "此节点改为未完成", method = "PUT")
    public Result<PlanScheduleContents> unaccomplished(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        PlanScheduleContents schedules = planScheduleContentsService.unaccomplished(id);
        return Result.OK(schedules);
    }

    @GetMapping("attach/{id}")
    @Operation(summary = "获取此节点附件", description = "获取此节点附件", method = "GET")
    public Result<List<PlanScheduleContentsAttach>> attachList(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        List<PlanScheduleContentsAttach> attachList = planScheduleContentsService.attachList(id);
        return Result.OK(attachList);
    }

    @PostMapping("attach/add")
    @Operation(summary = "新增节点附件", description = "新增节点附件", method = "POST")
    public Result<List<PlanScheduleContentsAttach>> add(@RequestBody ContentAttachVo contentAttachVo) {
        List<PlanScheduleContentsAttach> ls = planScheduleContentsService.saveAttach(contentAttachVo);
        return Result.OK(ls);
    }

    @DeleteMapping("attach/remove/{id}")
    @Operation(summary = "根据id删除附件", description = "根据id删除附件", method = "DELETE")
    public Result<String> removeAttach(@PathVariable(name = "id", required = true) String id) {
        planScheduleContentsService.removeAttach(id);
        return Result.OK();
    }

}
